#Author: Salvatore Gionfriddo
from SolusIpse import SolusIpse
from TicTacToeEnvironment import TicTacToeEnvironment
from HumanAgent import HumanAgent
from RandomAgent import RandomAgent
def play(turns):
    board = TicTacToeEnvironment()
    player_1 = SolusIpse()
    p1 = 0
    player_2 = RandomAgent()
    p2 = 0
    #Prepare the game
    player_1.Initialize(board.getState(), board.getAvailableActions())
    action = player_1.SelectNextAction()
    (state, reward, end) = board.performAction(action)
    player_2.Initialize(state, board.getAvailableActions())
    human = False
    print "Hello, I would like to play this game with you, but I do not understand the rules. I am going to clone myself and play against me for a bit. I will play with you shortly."
    for i in range(0,turns):
        action = player_2.SelectNextAction()
        (state, reward, end) = board.performAction(action)
        if end == 'Win' or end == 'Tie':
            #print 2
            p2 += 1
            player_2.ApplyResult(state, reward, board.getAvailableActions())
            if end == 'Win': end = 'Lose'
            player_1.ApplyResult(state, -reward, board.getAvailableActions())
            if i > turns-200:
                if not human:
                    print "I am ready to play against you now. I will show you all of the moves I thought about and finally the one I chose."
                    human = True
                    player_1.debug = True
                    player_2 = HumanAgent()
            #prepare a new game
            board.resetBoard()
            player_2.Initialize(board.getState(), board.getAvailableActions())
            action = player_2.SelectNextAction()
            (state, reward, end) = board.performAction(action)
            player_1.Initialize(state, board.getAvailableActions())
        else:
            player_1.ApplyResult(state, reward, board.getAvailableActions())
        action = player_1.SelectNextAction()
        if human:
            print "My move: ", action
        (state, reward, end) = board.performAction(action)
        if end == 'Win' or end == 'Tie':
            #print 1
            p1 += 1
            player_1.ApplyResult(state, reward, board.getAvailableActions())
            if end == 'Win': end = 'Lose'
            player_2.ApplyResult(state, -reward, board.getAvailableActions())
            if i > turns-200:
                if not human:
                    print "I am ready to play against you now. I will show you all of the moves I thought about and finally the one I chose."
                    human = True
                    player_1.debug = True
                    player_2 = HumanAgent()
            #prepare a new game
            board.resetBoard()
            player_1.Initialize(board.getState(), board.getAvailableActions())
            action = player_1.SelectNextAction()
            (state, reward, end) = board.performAction(action)
            player_2.Initialize(state, board.getAvailableActions())
        else:
            player_2.ApplyResult(state, reward, board.getAvailableActions())
        print 'end turn',i
    print end
    #print player_1.R

def main():
    for i in range(0,1):
        play(300)

main()
